package com.vercon.commonmirror.view.line_chart;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.widget.Scroller;


import com.vercon.commonmirror.R;
import com.vercon.commonmirror.utils.APPUtil;
import com.vercon.commonmirror.utils.MirrorLog;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import skin.support.utils.SkinPreference;

/**
 * Created by felix on 2016/11/10.
 * https://github.com/FelixLee0527/ZhiMaScoreCurve
 * 折线图
 */

public class ScoreTrend extends View {
    private final String TAG = getClass().getSimpleName();
    private float viewWidth;
    private float viewHeight;

    private final float brokenLineWith = 2f;
    private int brokenLineColor1 = 0xff494A51;
    private final int brokenLineColor2 = 0xff456125;
    private final int straightLineColor = 0xffe2e2e2;
    private final int textNormalColor = 0xffFFFFFF;

    private float maxScore = 800;
    private float minScore = 0;
    private float maxScoreYCoordinate = 0,minScoreYCoordinate = 0;

    //    private String[] monthText = new String[]{"1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"};
//    private int[] score = new int[]{660, 363, 669, 778, 682, 589, 669, 778, 682, 589, 363, 669, 363, 669, 778, 682, 589, 669, 778, 682, 589, 363, 669, 363, 669, 778, 682, 589, 669, 778, 682, 589, 363, 669};
//    private int[] score1 = new int[]{470, 263, 489, 568, 389, 269, 489, 568, 389, 269, 263, 489, 263, 489, 568, 389, 269, 489, 568, 389, 269, 263, 489, 263, 489, 568, 389, 269, 489, 568, 389, 269, 263, 489};
    private final List<String> monthText = new ArrayList();
    private List<Float> score = new ArrayList();
    private float xstarposition;

    {
        monthText.add("1");
        monthText.add("2");
        monthText.add("3");
        monthText.add("4");
        monthText.add("5");
        monthText.add("6");
        monthText.add("7");
        monthText.add("8");
        monthText.add("9");
        monthText.add("10");
        monthText.add("11");
        monthText.add("12");
        monthText.add("13");
        monthText.add("14");
        monthText.add("15");
        monthText.add("16");
        monthText.add("17");
        monthText.add("18");
        monthText.add("19");
        monthText.add("20");
        monthText.add("21");
        monthText.add("22");
        monthText.add("23");
        monthText.add("24");
        monthText.add("25");
        monthText.add("26");
        monthText.add("27");
        monthText.add("28");
        monthText.add("29");
        monthText.add("30");
        monthText.add("31");
        score.add(0f);
        score.add(0f);
        score.add(0f);
        score.add(0f);
        score.add(0f);
        score.add(0f);
        score.add(0f);
        score.add(0f);
        score.add(0f);
        score.add(0f);
        score.add(0f);
        score.add(0f);
        score.add(0f);
        score.add(0f);
        score.add(0f);
        score.add(0f);
        score.add(0f);
        score.add(0f);
        score.add(0f);
        score.add(0f);
        score.add(0f);
        score.add(0f);
        score.add(0f);
        score.add(0f);
        score.add(0f);
        score.add(0f);
        score.add(0f);
        score.add(0f);
        score.add(0f);
        score.add(0f);
        score.add(0f);
    }

    public void setData(List<Float> score1, List<Float> scor2) {
        empty = false;
        Log.e(TAG, "setData " + score1.toString());
        float min = 0, max = 0;
        this.score = score1;
        for (float i : score) {
            min = Math.min(min, i);
            max = Math.max(max, i);
        }
        for (float i : score) {
            min = Math.min(min, i);
            max = Math.max(max, i);
        }
        maxScore = max;
        minScore = min;
        if(maxScore <= 0) maxScore = 50;
        initData();
    }

    private boolean empty = true;

    public void reset(int year, int month) {
        setDate(year, month);
        totalDayInMonth = getMaxDayByYearMonth(year, month);
        empty = true;
        invalidate();
        Log.d(TAG, "reset " + empty);
    }

    private final int monthCount = monthText.size();
    private int selectMonth = 1;// 选中的月份

    private List<ScoreBean> scorePoints;

    private int textSize = dipToPx(15);

    private Paint brokenPaint;
    private Paint brokenPaint2;
    private Paint straightPaint;
    private Paint dottedPaint;
    private Paint textPaint;

    private Path brokenPath;
    private final Calendar calendar = Calendar.getInstance();
    private int year = calendar.get(Calendar.YEAR);
    private int month = calendar.get(Calendar.MONTH) + 1;
    private final int day = calendar.get(Calendar.DAY_OF_MONTH);
    private int totalDayInMonth;
    private Context mContext;

    public ScoreTrend(Context context) {
        super(context);
        this.mContext = context;
        initConfig(context, null);
        init();
    }

    public ScoreTrend(Context context, AttributeSet attrs) {
        super(context, attrs);
        initConfig(context, attrs);
        init();
    }

    public ScoreTrend(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initConfig(context, attrs);
        init();
    }

    /**
     * 初始化布局配置
     *
     * @param context
     * @param attrs
     */
    private void initConfig(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.widget_ScoreTrend);

        maxScore = a.getInt(R.styleable.widget_ScoreTrend_widget_max_score, 700);
        minScore = a.getInt(R.styleable.widget_ScoreTrend_widget_min_score, 650);
        brokenLineColor1 = a.getColor(R.styleable.widget_ScoreTrend_widget_broken_line_color, brokenLineColor1);
        a.recycle();
    }

    private void init() {
        mScroller = new Scroller(getContext());
        mVelocityTracker = VelocityTracker.obtain();
        brokenPath = new Path();

        brokenPaint = new Paint();
        brokenPaint.setAntiAlias(true);
        brokenPaint.setStyle(Paint.Style.STROKE);
        brokenPaint.setStrokeWidth(dipToPx(brokenLineWith));
        brokenPaint.setStrokeCap(Paint.Cap.ROUND);

        brokenPaint2 = new Paint();
        brokenPaint2.setAntiAlias(true);
        brokenPaint2.setStyle(Paint.Style.FILL);
        brokenPaint2.setStrokeWidth(dipToPx(brokenLineWith));
        brokenPaint2.setStrokeCap(Paint.Cap.ROUND);


        straightPaint = new Paint();
        straightPaint.setAntiAlias(true);
        straightPaint.setStyle(Paint.Style.STROKE);
        straightPaint.setStrokeWidth(brokenLineWith);
        straightPaint.setColor((brokenLineColor1));
        straightPaint.setStrokeCap(Paint.Cap.ROUND);

        dottedPaint = new Paint();
        dottedPaint.setAntiAlias(true);
        dottedPaint.setStyle(Paint.Style.STROKE);
        dottedPaint.setStrokeWidth(brokenLineWith);
        dottedPaint.setColor((straightLineColor));
        dottedPaint.setStrokeCap(Paint.Cap.ROUND);

        textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setColor((textNormalColor));
        textPaint.setTextSize(dipToPx(15));

    }

    float who;

    private void initData() {
        selectMonth = 1;
        scorePoints = new ArrayList<>();
        maxScoreYCoordinate = viewHeight * 0.3f;
        minScoreYCoordinate = viewHeight * 0.7f;
        who = viewWidth * 0.05f;

//        Log.v("ScoreTrend", "initData: " + maxScoreYCoordinate);

        float newWith = viewWidth - (who) * 2;//分隔线距离最左边和最右边的距离是0.15倍的viewWith
        totalDayInMonth = getMaxDayByYearMonth(year, month);
        int coordinateX;
        //折线起始位置
        xstarposition = newWith * ((float) (1) / (totalDayInMonth - 1));
        //第一条线数据的初始化
        for (int i = 0; i < totalDayInMonth; i++) {
            Log.v("ScoreTrend", "initData: " + score.get(i));
            ScoreBean bean = new ScoreBean();
            Point point = new Point();
            coordinateX = (int) (newWith * ((float) (i) / (totalDayInMonth - 1)) + (who)+ xstarposition);
            point.x = coordinateX;
            if (score!= null&&score.size() >i) {
                if (score.get(i) > maxScore) {
                    score.set(i, maxScore);
                } else if (score.get(i) < minScore) {
                    score.set(i, minScore);
                }
            }else {
               return;
            }
            point.y = (int) (((float) (maxScore - score.get(i)) / (maxScore - minScore)) * (minScoreYCoordinate - maxScoreYCoordinate) + maxScoreYCoordinate);
            bean.setPoint(point);
            bean.setId(1);
            bean.setMonth(selectMonth + i);
            bean.setSourceIndex(i);
            bean.setClick(false);
            scorePoints.add(bean);
        }

        //第二条线数据的初始化
       /* for (int i = 0; i < score1.size(); i++) {
//            Log.v("ScoreTrend", "initData: " + score1.get(i));
            ScoreBean bean = new ScoreBean();
            Point point = new Point();
            coordinateX = (int) (newWith * ((float) (i) / (monthCount - 1)) + (who));
            point.x = coordinateX;
            if (score1.get(i) > maxScore) {
                score1.set(i, maxScore);
            } else if (score1.get(i) < minScore) {
                score1.set(i, minScore);
            }
            point.y = (int) (((float) (maxScore - score1.get(i)) / (maxScore - minScore)) * (minScoreYCoordinate - maxScoreYCoordinate) + maxScoreYCoordinate);
            bean.setPoint(point);
            bean.setId(2);
            bean.setMonth(selectMonth + i);
            bean.setSourceIndex(i);
            bean.setClick(false);
            scorePoints.add(bean);
        }*/
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        viewWidth = w;
        viewHeight = h;
        who = viewWidth * 0.05f;
        //initData();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Log.d("MeasurementFragment", "onDraw");
        super.onDraw(canvas);
        // drawDottedLine(canvas, who, viewHeight * 0.15f, viewWidth, viewHeight * 0.15f);
        // drawDottedLine(canvas, who, viewHeight * 0.4f, viewWidth, viewHeight * 0.4f);
        drawText(canvas);
        drawMonthLine(canvas);
        drawXLine(canvas);
        Log.d(TAG, "onDraw " + empty);
        if (empty) return;
        drawBrokenLine(canvas);
//        drawBrokenSecondLine(canvas);
        drawPoint(canvas);
    }

    private Scroller mScroller;
    private VelocityTracker mVelocityTracker;
    private float mXMove;
    private float mXLastMove;
    // 上次滑动的坐标
    private float mLastX = 0;
    private float mLastY = 0;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // 一旦底层View收到touch的action后调用这个方法那么父层View就不会再调用onInterceptTouchEvent了，也无法截获以后的action
        this.getParent().requestDisallowInterceptTouchEvent(true);
//        mVelocityTracker.addMovement(event);
        float x = event.getX();
        float y = event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.d(TAG, "ACTION_DOWN");
                if (!mScroller.isFinished()) mScroller.abortAnimation();
                break;
            case MotionEvent.ACTION_MOVE:
                Log.d(TAG, "ACTION_MOVE");
                float detalX = x - mLastX;
                float detalY = y - mLastY;
//                scrollBy((int) -detalX, 0);
                break;
            case MotionEvent.ACTION_UP:
                Log.d(TAG, "ACTION_UP");
                onActionUpEvent(event);
//                this.getParent().requestDisallowInterceptTouchEvent(false);
//                float scrollX = getScrollX();
//                mVelocityTracker.computeCurrentVelocity(1000);
//                float xVelocity = mVelocityTracker.getXVelocity();
//                if (Math.abs(xVelocity) >= 50) {
//                    float dx = xVelocity - scrollX;
//                    smoothScroll((int) dx, 0);
//                }
//                mVelocityTracker.clear();
                break;
            case MotionEvent.ACTION_CANCEL:
                this.getParent().requestDisallowInterceptTouchEvent(false);
                break;
        }
        mLastX = x;
        mLastY = y;
        return true;
    }

    public void smoothScroll(int dx, int dy) {
        //设置滑动参数
        mScroller.startScroll(getScrollX(), 0, dx, 0, 1000);
        //重新绘制View
        invalidate();
    }

    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            scrollBy(mScroller.getCurrX(), mScroller.getCurrY());
            invalidate();
        }
    }

    private void onActionUpEvent(MotionEvent event) {
        Point rawPoint = new Point();
        rawPoint.x = (int) event.getRawX();
        rawPoint.y = (int) event.getRawY();
        boolean isValidTouch = validateTouch(event.getX(), event.getY(), rawPoint);

        if (isValidTouch) {
            invalidate();
        }
    }

    int touch_x,touch_y;//点击中点，x,y坐标
    //是否是有效的触摸范围
    private boolean validateTouch(float x, float y, Point rawPoint) {
        if (scorePoints==null) return false;
        // 曲线触摸区域
        int line = dipToPx(7) * 2;// 正方形边长
        int aa = (int) (((float) (maxScore ) / (maxScore - minScore)) * (minScoreYCoordinate - maxScoreYCoordinate) + maxScoreYCoordinate);//这是y坐标为0时候，y的值
        for (int i = 0; i < scorePoints.size(); i++) {
            // dipToPx(8)乘以2为了适当增大触摸面积
            if (x > (scorePoints.get(i).getPoint().x - line) && x < (scorePoints.get(i).getPoint().x + line)) {
                if (y > (scorePoints.get(i).getPoint().y - line) && y < (scorePoints.get(i).getPoint().y + line)) {
                    scorePoints.get(i).setClick(true);
                    selectMonth = scorePoints.get(i).getMonth();

                    brokenPaint.setColor(brokenLineColor1);
                    Log.e("ScoreTrend", "点数 ==" + scorePoints.size());
                    touch_x = scorePoints.get(i).getPoint().x;
                    touch_y = scorePoints.get(i).getPoint().y;
                    if (aa > touch_y) {
                        // 点击回调
                        mListener.onPoint(i < (scorePoints.size() / 2), i, scorePoints.get(i).getPoint(), rawPoint);
                        invalidate();
                    }else {
                        return false;
                    }
                    return true;
                }
            }
        }

        // FIXME: 2019/10/9 取消份触摸区域
//        //计算每个月份X坐标的中心点
//        float monthTouchY = viewHeight * 0.7f - dipToPx(3);//减去dipToPx(3)增大触摸面积
//
//        float newWith = viewWidth - (who) * 2;//分隔线距离最左边和最右边的距离是0.15倍的viewWith
//        float validTouchX[] = new float[monthText.size()];
//        for (int i = 0; i < monthText.size(); i++) {
//            validTouchX[i] = newWith * ((float) (i) / (monthCount - 1)) + (who);
//        }
//
//        if (y > monthTouchY) {
//            for (int i = 0; i < validTouchX.length; i++) {
//                Log.v("ScoreTrend", "validateTouch: validTouchX:" + validTouchX[i]);
//                if (x < validTouchX[i] + dipToPx(8) && x > validTouchX[i] - dipToPx(8)) {
//                    Log.v("ScoreTrend", "validateTouch: " + (i + 1));
//                    selectMonth = scorePoints.get(i).getMonth();
//                    return true;
//                }
//            }
//        }

        return false;
    }


    // 绘制折线穿过的点
    protected void drawPoint(Canvas canvas) {
        if (scorePoints == null || score.size() < 0) {
            return;
        }
        brokenPaint.setStrokeWidth(dipToPx(2));
        brokenPaint.setStyle(Paint.Style.FILL);
        brokenPaint.setColor(brokenLineColor1);
        Log.e("ScoreTrend", "点数 ==" + scorePoints.size());
        for (int i = 0; i < scorePoints.size(); i++) {
            if (scorePoints.get(i).getMonth() == selectMonth && scorePoints.get(i).isClick()) {
                scorePoints.get(i).setClick(false);
            }

            if (scorePoints.get(i).getPoint().x == touch_x && scorePoints.get(i).getPoint().y == touch_y){
                continue;
            }
            canvas.drawCircle(scorePoints.get(i).getPoint().x, scorePoints.get(i).getPoint().y, dipToPx(3f), brokenPaint);
        }
        Log.e("ScoreTrend", "touch_x ==" + touch_x+"--touch_y ==" + touch_y);
        if (touch_x > 0) {
            brokenPaint2.setColor(textNormalColor);
            canvas.drawCircle(touch_x, touch_y, dipToPx(3f), brokenPaint2);
        }
    }

    private void DrawText(Canvas canvas, float x, float y) {
//        canvas.drawText("含水量", x - dipToPx(40), y - dipToPx(50), textPaint);
    }

    // 绘制月份的直线(包括刻度)
    private void drawMonthLine(Canvas canvas) {
        straightPaint.setStrokeWidth(dipToPx(1));
        float newWith = viewWidth - (who) * 2;//分隔线距离最左边和最右边的距离是0.15倍的viewWith
//        canvas.drawLine(who, viewHeight * 0.7f, viewWidth, viewHeight * 0.7f, straightPaint);
        canvas.drawLine(who, viewHeight * 0.7f, newWith * score.size(), viewHeight * 0.7f, straightPaint);

        float coordinateX;//分隔线X坐标
        for (int i = 1; i < totalDayInMonth + 1; i++) {
            coordinateX = newWith * ((float) (i) / (totalDayInMonth - 1)) + (who);
            canvas.drawLine(coordinateX, viewHeight * 0.7f, coordinateX, viewHeight * 0.7f + dipToPx(4), straightPaint);
        }
    }

    private void drawXLine(Canvas canvas) {
        straightPaint.setStrokeWidth(dipToPx(1));
        canvas.drawLine(who, viewHeight * 0.7f, who, viewHeight * 0.2f, straightPaint);
    }

    //绘制折线
    private void drawBrokenLine(Canvas canvas) {
        brokenPath.reset();
        brokenPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        brokenPaint.setColor(brokenLineColor1);
        brokenPaint.setShader(new LinearGradient(getWidth() / 2f, 0f, getWidth() / 2f, viewHeight * 0.7f,
                getThemeColor(1), getThemeColor(2), Shader.TileMode.CLAMP));

//        brokenPaint.setStrokeWidth(dipToPx(2));
        if (score.size() == 0) {
            return;
        }
        Log.v("ScoreTrend", "drawBrokenLine: " + scorePoints.size());
        boolean isFirst = true;
        for (int i = 0; i < scorePoints.size(); i++) {
            if (scorePoints.get(i).getId() == 1) {
                if (isFirst) {
                    brokenPath.moveTo(who, viewHeight * 0.7f);
//                    brokenPath.moveTo(scorePoints.get(i).getPoint().x, scorePoints.get(i).getPoint().y);
                    isFirst = false;
                }
                brokenPath.lineTo(scorePoints.get(i).getPoint().x, scorePoints.get(i).getPoint().y);
                Log.v("ScoreTrend", "lineTo x=" + scorePoints.get(i).getPoint().x+"--lineTo y=" + scorePoints.get(i).getPoint().y);
            }
        }
        Log.v("ScoreTrend", "drawBrokenLine: drawPath ");
        if (scorePoints.size() > 0) {
            int y = (int) (minScoreYCoordinate - maxScoreYCoordinate + maxScoreYCoordinate);
            brokenPath.lineTo(scorePoints.get(scorePoints.size() - 1).getPoint().x, y);//todo dht 新增的，lineTo最后一个点位，这些才能形成闭环画图才不会错乱
        }
        canvas.drawPath(brokenPath, brokenPaint);
    }

    //绘制折线
    private void drawBrokenSecondLine(Canvas canvas) {
        brokenPath.reset();
        brokenPaint.setColor(brokenLineColor2);
        brokenPaint.setStyle(Paint.Style.STROKE);
        if (score.size() == 0) {
            return;
        }
        Log.v("ScoreTrend", "drawBrokenLine: " + scorePoints.get(0));
        boolean isFirst = true;
        for (int i = 0; i < scorePoints.size(); i++) {
            if (scorePoints.get(i).getId() == 2) {
                if (isFirst) {
                    brokenPath.moveTo(scorePoints.get(i).getPoint().x, scorePoints.get(i).getPoint().y);
                    isFirst = false;
                }
                brokenPath.lineTo(scorePoints.get(i).getPoint().x, scorePoints.get(i).getPoint().y);
            }
        }

        canvas.drawPath(brokenPath, brokenPaint);
    }

    // 绘制文本
    private void drawText(Canvas canvas) {
        textPaint.setTextSize(dipToPx(16));
        textPaint.setColor(textNormalColor);
        if (score != null&& score.size()> 0) {

            // 绘制纵坐标第一个点的数字
            String pointY1 = String.valueOf(score.get(0));
//        String pointY2 = String.valueOf(score1[0]);
    //        canvas.drawText(pointY1, scorePoints.get(0).getPoint().x - dipToPx(this.getResources().getDimension(R.dimen.dp_23)), scorePoints.get(0).getPoint().y + dipToPx(2), textPaint);
//        canvas.drawText(pointY2, scorePoints.get(score.length).getPoint().x - dipToPx(22), scorePoints.get(score.length).getPoint().y + dipToPx(2), textPaint);

            textPaint.setColor(0xff7c7c7c);

            float newWith = viewWidth - (who) * 2;//分隔线距离最左边和最右边的距离是0.15倍的viewWith
            float coordinateX;//分隔线X坐标
            textPaint.setTextSize(dipToPx(12));
            textPaint.setStyle(Paint.Style.FILL);
            textPaint.setColor(textNormalColor);
            textSize = (int) textPaint.getTextSize();
            float Xstarposition = newWith * ((float) (1) / (totalDayInMonth - 1)); //月份文字起始位置
            for (int i = 0; i < totalDayInMonth; i++) {
                coordinateX = newWith * ((float) (i) / (totalDayInMonth - 1)) + (who) + Xstarposition;
//            Log.d(TAG, "selectMonth = " + selectMonth +" i " + i);
            /*if (i == selectMonth - 1) {
                textPaint.setStyle(Paint.Style.STROKE);
                textPaint.setColor(brokenLineColor1);
                RectF r2 = new RectF();
                r2.left = coordinateX - textSize - dipToPx(4);
                r2.top = viewHeight * 0.7f + dipToPx(4) + textSize / 2;
                r2.right = coordinateX + textSize + dipToPx(4);
                r2.bottom = viewHeight * 0.7f + dipToPx(4) + textSize + dipToPx(8);
                canvas.drawRoundRect(r2, 10, 10, textPaint);
            }*/
                //绘制月份
                canvas.drawText(monthText.get(i), coordinateX, viewHeight * 0.7f + dipToPx(4) + textSize + dipToPx(5), textPaint);

                textPaint.setColor(textNormalColor);
            }
        }
    }

    //绘制显示浮动文字的背景
    private void drawFloatTextBackground(Canvas canvas, int x, int y) {
        brokenPath.reset();
        brokenPaint.setColor(brokenLineColor1);
        brokenPaint.setStyle(Paint.Style.FILL);

        //P1
        Point point = new Point(x - dipToPx(50), y);
        brokenPath.moveTo(point.x, point.y);

        //P2
        point.x = point.x + dipToPx(100);
        brokenPath.lineTo(point.x, point.y);

        //P3
        point.y = point.y - dipToPx(50);
        brokenPath.lineTo(point.x, point.y);

        //P4
        point.x = point.x - dipToPx(100);
        brokenPath.lineTo(point.x, point.y);


        //最后一个点连接到第一个点
        brokenPath.lineTo(x - dipToPx(50), y);

        canvas.drawPath(brokenPath, brokenPaint);
    }

    /**
     * 画虚线
     *
     * @param canvas 画布
     * @param startX 起始点X坐标
     * @param startY 起始点Y坐标
     * @param stopX  终点X坐标
     * @param stopY  终点Y坐标
     */
    private void drawDottedLine(Canvas canvas, float startX, float startY, float stopX, float stopY) {
        dottedPaint.setPathEffect(new DashPathEffect(new float[]{20, 10}, 4));
        dottedPaint.setStrokeWidth(0.5f);
        // 实例化路径
        Path mPath = new Path();
        mPath.reset();
        // 定义路径的起点
        mPath.moveTo(startX, startY);
        mPath.lineTo(stopX, stopY);
        canvas.drawPath(mPath, dottedPaint);
    }



    public List<Float> getScore() {
        return score;
    }

    /*public void setScore(List<Float> score, List<Float> score1) {
        this.score = score;
        this.score1 = score1;
        initData();
    }*/

    public void setMaxScore(int maxScore) {
        this.maxScore = maxScore;
    }

    public void setMinScore(int minScore) {
        this.minScore = minScore;
    }

    /**
     * dip 转换成px
     *
     * @param dip
     * @return
     */
    private int dipToPx(float dip) {
        float density = getContext().getResources().getDisplayMetrics().density;
        return (int) (dip * density + 0.5f * (dip >= 0 ? 1 : -1));
    }

//
//    interface OnPointClick {
//        void onPoint(Boolean firstOrSecond, int position, Point point);
//    }

    private OnPointClick mListener;

    public void setOnPointClickListener(OnPointClick listener) {
        this.mListener = listener;
    }

    /**
     * 获得某个月最大天数
     *
     * @param year  年份
     * @param month 月份 (1-12)
     * @return 某个月最大天数
     */
    private int getMaxDayByYearMonth(int year, int month) {
        calendar.set(Calendar.DATE, 1);
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        return calendar.getActualMaximum(Calendar.DATE);
    }

    public void setDate(int year, int month) {
        this.year = year;
        this.month = month;
    }

    public void resetPointClickState(){
        touch_x = 0;
        touch_y = 0;
        invalidate();
    }


    public void showPopWindow() {
        if (mListener != null) {
            if (score != null&&score.size() > day - 1) {
                int position = NewestPosition(day - 1);
                //if (position == 0) return;
                Point point = null;
                if(mContext != null && APPUtil.isAndroid_11(mContext)){
                    point = new Point((int) (scorePoints.get(position).getPoint().x + xstarposition+20), (int) (scorePoints.get(position).getPoint().y +60 +this.getHeight()));
                }else{
                     point = new Point((int) (scorePoints.get(position).getPoint().x + xstarposition+20), (int) (scorePoints.get(position).getPoint().y +getResources().getDimension(R.dimen.dp_140) +this.getHeight()));
                }

                mListener.onPoint(true, position, scorePoints.get(position).getPoint(), point);
                touch_x =  scorePoints.get(position).getPoint().x;
                touch_y = scorePoints.get(position).getPoint().y;
                scorePoints.get(position).setClick(true);
                invalidate();
            }
        }
    }

    public int NewestPosition(int position){
        if (score.get(position) <= 0 && position>0){
            return NewestPosition(position-1);
        }else {
            return position;
        }
    }

    //获取不同主题的曲线颜色
    public int getThemeColor(int state) {
        String skinName = SkinPreference.getInstance().getSkinName();
        int colorValue = Color.parseColor("#4DBE8D");
        switch (state) {
            case 1://曲线颜色渐变色1
                if (skinName.equals("gold")) {
                    colorValue = Color.parseColor("#CCFF9F72");
                } else {
                    colorValue = Color.parseColor("#CC4DBE8D");
                }
                break;
            case 2://曲线颜色渐变色2
                if (skinName.equals("gold")) {
                    colorValue = Color.parseColor("#00FF9F72");
                } else {
                    colorValue = Color.parseColor("#004DBE8D");
                }
                break;
        }
        return colorValue;
    }
}
